package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.basedata.BaseArea;
import org.jeecg.entity.basedata.HouseHolde;
import org.jeecg.entity.charge.ChargeBills;
import org.jeecg.modules.basedata.mapper.BaseHouseHoldeMapper;
import org.jeecg.modules.basedata.service.IBaseHouseHoldeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 区域
 * @Author: jeecg-boot
 * @Date:   2019-06-14
 * @Version: V1.0
 */
@Service
public class BaseHouseHoldeServiceImpl extends ServiceImpl<BaseHouseHoldeMapper, BaseArea> implements IBaseHouseHoldeService {

    @Autowired
    private BaseHouseHoldeMapper baseHouseHoldeMapper;

    public Page<HouseHolde> getHouseholdList(Page<HouseHolde> page, HouseHolde houseHolde){
        SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");
        List<HouseHolde> list = new ArrayList<HouseHolde>();
        if(houseHolde != null && houseHolde.getFloorId() != null && houseHolde.getFloorId().size() > 0){
            list = baseHouseHoldeMapper.getHouseholdList(page,houseHolde);
            for (HouseHolde holde:list){
                String cusId=  baseHouseHoldeMapper.selectByCustomerId(holde.getRoomId());
                ChargeBills bills = baseHouseHoldeMapper.getMaxDateInBills(cusId);
                try{
                    if (bills != null && StringUtils.isNotEmpty(bills.getZjCny())){
                        holde.setCurrentChargeTime(sdf.parse(bills.getZjCny()+"-01"));
                    }
                } catch (ParseException e){
                    e.printStackTrace();
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    public List<HouseHolde> getHouseholdListNotPage(HouseHolde houseHolde){
        SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");
        List<HouseHolde> list = new ArrayList<HouseHolde>();
        if(houseHolde != null && houseHolde.getFloorId() != null && houseHolde.getFloorId().size() > 0){
            list = baseHouseHoldeMapper.getHouseholdList(houseHolde);
            for (HouseHolde holde:list){
                String cusId=  baseHouseHoldeMapper.selectByCustomerId(holde.getRoomId());
                ChargeBills bills = baseHouseHoldeMapper.getMaxDateInBills(cusId);
                try{
                    if (bills != null && StringUtils.isNotEmpty(bills.getZjCny())){
                        holde.setCurrentChargeTime(sdf.parse(bills.getZjCny()+"-01"));
                    }
                } catch (ParseException e){
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

}
